package com.easylinkin.linkappapi.devicedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.alarm.entity.Alarm;
import com.easylinkin.linkappapi.alarm.mapper.AlarmMapper;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.service.DeviceService;
import com.easylinkin.linkappapi.deviceattributestatus.entity.DeviceAttributeStatus;
import com.easylinkin.linkappapi.deviceattributestatus.service.DeviceAttributeStatusService;
import com.easylinkin.linkappapi.devicedata.service.DeviceDataService;
import com.easylinkin.linkappapi.space.entity.LinkappSpace;
import com.easylinkin.linkappapi.space.mapper.LinkappSpaceMapper;
import com.easylinkin.linkappapi.space.service.LinkappSpaceService;
import com.easylinkin.linkappapi.tenant.entity.LinkappTenant;
import com.easylinkin.linkappapi.tenant.mapper.LinkappTenantMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceAttributeStatusService deviceAttributeStatusService;

    @Resource
    LinkappSpaceMapper linkappSpaceMapper;

    @Resource
    AlarmMapper alarmMapper;

    @Resource
    private LinkappTenantMapper linkappTenantMapper;

    @Override
    public RestMessage getDeviceDetail(Map<String, String> map) {
        Map<String, Object> result = new HashMap<>();

        // 1、获取设备基本信息
        Device device = new Device();
        device.setId(map.get("id"));
        Device device1 = deviceService.selectDevices(device).get(0);
        result.put("deviceBaseInfo", device1);

        // 2、获取设备状态信息
        List<DeviceAttributeStatus> deviceDataList = deviceAttributeStatusService.getDeviceDataByProject(map);
        result.put("deviceStatusInfo", deviceDataList);


        // 3、获取设备服务信息 待定

        return RestBuilders.successBuilder().data(result).build();

    }

    @Override
    public IPage<Alarm> getAlarmInfo(Page page, Alarm alarm) {
        List<LinkappSpace> linkappSpaces = new ArrayList<>();
        List<String> spacesIds = new ArrayList<>();
        linkappSpaces = linkappSpaceMapper.selectLinkappSpaceList(new LinkappSpace());
        if (ObjectUtils.isNotEmpty(linkappSpaces)) {
            spacesIds = linkappSpaces.stream().map(LinkappSpace::getId).collect(Collectors.toList());
        }
        // 通过城建项目编号查询施工云项目租户ID
        QueryWrapper<LinkappTenant> qw = new QueryWrapper<>();
        qw.eq("project_code_", alarm.getProjectCode());
        String tenantId = linkappTenantMapper.selectOne(qw).getId();
        alarm.setTenantId(tenantId);
        return alarmMapper.getAlarms(page, alarm, spacesIds);
    }
}
